#include "Adlogic.h"
#include "AdDao.h"

Ad *parseUpDownSplitScr(cJSON *item){
    /*  
                "id":1,
                "template_id":6,
                "customer_id":1,
                "top_media_type_id":1,
                "top_media_url":"http://localhost:80/1.mp4",
                "top_media_sign":"jfjjfjfjj",
                "buttom_media_type_id":2,
                "buttom_media_url":"http://localhost:80/add.png",
                "buttom_media_sign":"jfjjfjfjj",
                "start_time": "2024-06-01 00:00:00",
                "end_time": "2024-06-30 23:59:59",
                "state":0

    */
    cJSON *id = cJSON_GetObjectItemCaseSensitive(item,"id");
    cJSON *template_id = cJSON_GetObjectItemCaseSensitive(item,"template_id");
    cJSON *customer_id = cJSON_GetObjectItemCaseSensitive(item,"customer_id");
    cJSON *top_media_type_id = cJSON_GetObjectItemCaseSensitive(item,"top_media_type_id");
    cJSON *top_media_url = cJSON_GetObjectItemCaseSensitive(item,"top_media_url");
    cJSON *top_media_sign = cJSON_GetObjectItemCaseSensitive(item,"top_media_sign");
    cJSON *buttom_media_type_id = cJSON_GetObjectItemCaseSensitive(item,"buttom_media_type_id");
    cJSON *buttom_media_url = cJSON_GetObjectItemCaseSensitive(item,"buttom_media_url");
    cJSON *buttom_media_sign = cJSON_GetObjectItemCaseSensitive(item,"buttom_media_sign");
    cJSON *start_time = cJSON_GetObjectItemCaseSensitive(item,"start_time");
    cJSON *end_time = cJSON_GetObjectItemCaseSensitive(item,"end_time");
    cJSON *state = cJSON_GetObjectItemCaseSensitive(item,"state");

    //用结构体来装这些数据
    Ad * result = calloc(1,sizeof(Ad));
    result->id=id->valueint;  
    result->templateId  = template_id->valueint;
    result->customerId  = customer_id->valueint;
    result->start_time  = start_time->valuestring;
    result->end_time  = end_time->valuestring;

    //广告资源解析
    AdResource *upAr = calloc(1,sizeof(AdResource));
    upAr->ad_id = id->valueint;
    upAr->index = 0;
    upAr->type_id = top_media_type_id->valueint;
    upAr->path = top_media_url->valuestring;
    upAr->sign = top_media_sign->valuestring;   
    result->resources[0] =upAr;

    AdResource *buttomAr = calloc(1,sizeof(AdResource));
    buttomAr->ad_id = id->valueint;
    buttomAr->index = 1;
    buttomAr->type_id = buttom_media_type_id->valueint;
    buttomAr->path = buttom_media_url->valuestring;
    buttomAr->sign = buttom_media_sign->valuestring;        
    result->resources[1] =buttomAr;
    //第三个没有值了，就不赋值了
    result->resources[2] =NULL;

    return result;
}
//数据处理
Ad *paresAdItem(cJSON *item)
{
     //获取出当前的广告是什么分屏模式
    cJSON *tmepId = cJSON_GetObjectItemCaseSensitive(item,"template_id");
    int template_id = tmepId->valueint;
    Ad *result;
    switch (template_id)
    {
    case 1:
        
        break;
    case 2:
        
        break;
    case 6://上下分屏模式
        //调用上下分屏数据解析的函数
        result=parseUpDownSplitScr(item);
        
        break;        
    
    default:
        break;
    }
    
    return result;
}

//广告业务逻辑
void paresAdList(cJSON *ad_list)
{
    //判断ad_list是不是数组
    if (cJSON_IsArray(ad_list))
    {
        //遍历
        int size=cJSON_GetArraySize(ad_list);
        //用数组存结构体数据
        Ad *ads[size];
        for (int i = 0; i < size; i++)
        {
            //依次取数数据
            cJSON *item= cJSON_GetArrayItem(ad_list,i);
            //解析数据
            Ad *ad = paresAdItem(item);
            ads[i] = ad;
        }
        
        //完成数据的存储和文件的解析
        //2 打印远程播放列表
        for (int i = 0; i < size; i++){
            Ad* ad= ads[i];
            if (ad==NULL)
            {
                continue;
            }

            printf("id=%d,template_id=%d\n",ad->id,ad->templateId);
           for (int j = 0; j < 3; j++)
           {
               AdResource *ar = ad->resources[j];
               if (ar==NULL)
               {
                  continue;
               }  
               printf("id=%d,path=%s,sign=%s\n",ar->id,ar->path,ar->sign);
           }
           printf("==========================\n");   
        }
    }
    
}